<?php

namespace app\common\cachemodel\zl8;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class Zl8TarotAiAnswerRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl8_tarot_ai_answer_record';

    protected $cacheModel = 'divination.tarot_card_ai_answer';

    public $officialAvatar = '/test/ai_answer_avatar.png';

    /**
     * @param array $data
     * @return int
     */
    public function SaveData(array $data)
    {
        try
        {
            $id = $this->insertGetId($data);

            $this->listDataAdd($data['tarot_id'], [$id => $id]);
            return (int)$id;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return 0;
        }
    }

    /**
     * 更新 信息
     * @param int $id
     * @param array $data
     * @return bool
     */
    public function UpdateData(int $id, array $data)
    {
        try
        {
            $data['update_time'] = time();
            $this->where(['id' => $id])->update($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->rmCache();
            return true;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        if ($ids)
        {   // 获取未删除 的数据
            $where = ['id' => ['in', $ids]];
        } else
        {   // 获取指定pid  未删除 且 未隐藏的数据
            $where = ['tarot_id' => $type[0]];
        }
        $field = 'uid,status,tarot_id,location,num_type,content,create_time,update_time';
        try
        {
            $data = $this->field($field)
                ->where($where)
                ->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'uid', 'tarot_id', 'status', 'location', 'num_type', 'create_time', 'update_time']);
        }

        return $data;
    }

    /**
     * 数据 重组
     * @return array
     */
    public function dataResert(array $tarotInfo, array $aiAnswerDataList)
    {
        if (!$aiAnswerDataList)
        {
            return [];
        }
        $tarotConfig = config('tarot');
        $cardInfo = array_column($tarotInfo['content'], null, 'location');
        $resData = [];
        foreach ($aiAnswerDataList as $item)
        {
            $cardDatas = [];
            if ($item['location'] === -1)
            {
                $cardDatas = $cardInfo;
            } else
            {
                $cardDatas[] = $cardInfo[$item['location']];
            }
            $cardListItem = [];
            foreach ($cardDatas as $cardItem)
            {
                $cardListItem[] = [
                    'img' => "/test/tarot/cards/{$cardItem['card_id']}-{$cardItem['direction']}.png",
                    'name' => $tarotConfig['card_list'][$cardItem['card_id']]['name'] . '·' . ((int)$cardItem['direction'] === 1 ? '正位' : '逆位'),
                    'tip' => $tarotConfig['tarot_type'][$tarotInfo['tarot_type']][$cardItem['location']]['tip'],
                    'location' => $cardItem['location'],
                ];
            }
            $resData[] = [
                'card_list' => $cardListItem,
                'answer_content' => $item['content'],
                'id' => $item['id'],
                'status' => $item['status'] + 1,
                'btn_info' => null,
            ];
        }

        return $resData;
    }

    /**
     * @param array|null $delayUnlockCard
     * @return array|null
     */
    public function delayUnlockData($delayUnlockCard, int $tarotType, int $num)
    {
        if (!$delayUnlockCard)
        {
            return null;
        }
        $tarotConfig = config('tarot');
        $btnName = 'AI准准解读';
        if ($num)
        {
            $btnName .= "({$num}次)";
        }
        $resData = [
            'card_list' => [
                [
                    'img' => "/test/tarot/cards/{$delayUnlockCard['card_id']}-{$delayUnlockCard['direction']}.png",
                    'name' => $tarotConfig['card_list'][$delayUnlockCard['card_id']]['name'] . '·' . ((int)$delayUnlockCard['direction'] === 1 ? '正位' : '逆位'),
                    'tip' => $tarotConfig['tarot_type'][$tarotType][$delayUnlockCard['location']]['tip'],
                    'location' => $delayUnlockCard['location'],
                ],
            ],
            'answer_content' => '',
            'id' => 0,
            'status' => 0,
            'btn_info' => [
                'icon' => '/test/tarot/tarot_ai_answer_icon.png',
                'btn_name' => $btnName,
            ],
        ];
        return $resData;
    }
}